
// <copyright file="ThreadPoolSettings.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The thread limits.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Threading
{
    using System.Threading;

    /// <summary>
    /// The thread limits.
    /// </summary>
    public class ThreadLimits
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the completion port threads.
        /// </summary>
        public int? CompletionPortThreads { get; set; }

        /// <summary>
        /// Gets or sets the worker threads.
        /// </summary>
        public int? WorkerThreads { get; set; }

        #endregion Public Properties
    }

    /// <summary>
    /// The thread pool settings.
    /// </summary>
    public sealed class ThreadPoolSettings
    {
        #region Static Fields

        /// <summary>
        /// The instance.
        /// </summary>
        private static readonly ThreadPoolSettings InstanceObject = new ThreadPoolSettings();

        #endregion Static Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Prevents a default instance of the <see cref="ThreadPoolSettings"/> class from being created.
        /// </summary>
        private ThreadPoolSettings()
        {
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static ThreadPoolSettings Instance
        {
            get
            {
                return InstanceObject;
            }
        }

        /// <summary>
        /// Gets or sets the max threads.
        /// </summary>
        public ThreadLimits MaxThreads { get; set; }

        /// <summary>
        /// Gets or sets the min threads.
        /// </summary>
        public ThreadLimits MinThreads { get; set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The apply.
        /// </summary>
        public void Apply()
        {
            if (this.MinThreads != null && (this.MinThreads.WorkerThreads != null || this.MinThreads.CompletionPortThreads != null))
            {
                int minWorkerThreads;
                int minCompletionPortThreads;
                ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
                ThreadPool.SetMinThreads(this.MinThreads.WorkerThreads ?? minWorkerThreads, this.MinThreads.CompletionPortThreads ?? minCompletionPortThreads);
            }

            if (this.MaxThreads != null && (this.MaxThreads.WorkerThreads != null || this.MaxThreads.CompletionPortThreads != null))
            {
                int maxWorkerThreads;
                int maxCompletionPortThreads;
                ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
                ThreadPool.SetMaxThreads(this.MaxThreads.WorkerThreads ?? maxWorkerThreads, this.MaxThreads.CompletionPortThreads ?? maxCompletionPortThreads);
            }
        }

        #endregion Public Methods and Operators
    }
}
